<?php
Class UserController extends Controller{

	public function show($id){
		$user = $this->loader->model('user');
		$user->SelectUserFromSql($id);
		$this->smarty->assign('user', $user);
		$this->smarty->assign('title', 'Kémia éve - Profil');
		$this->smarty->assign('id',$id);
		$this->smarty->assign('session_id',$_SESSION['user_id']);
	}
	
	public function resetpassword(){
		$user = $this->loader->model('user');
		if(isset($_POST['submit'])){
			$url = Tools::get_href('?q=user/resetlink/');
			$mail = new Mail();
			$id = $user->GetIdFromSQLByEmail($_POST['email']);
			$link = $user->GeneratePasswordResetLink();
			$user->InsertPasswordResetLink($id[0]['id'], $link);
			$mail->SendNewPasswordMail($_POST['email'], $link, $url);
			$notifications['mailsent']='Az e-mailt kiküldtük!';
		}
		$this->smarty->assign('notifications',$notifications);
	}
	
	public function resetlink($link){
		$user = $this->loader->model('user');
		$user_id = $user -> IsThisAResetLink($link);
		if(isset($user_id)){
			$user -> DeleteResetLink($link);
			$temp=$user -> SelectUserFromSql($user_id[0]['user_id']);
			$_SESSION['username'] = $temp[0]['username'];
			$_SESSION['user_id'] = $user_id[0]['user_id'];
			$_SESSION['is_teacher'] = $temp[0]['is_teacher'];
			Routing::redirect('?q=user/editpassword/'.$user_id[0]['user_id']);
		}
	}
	
	public function editpassword($id){
		$user = $this->loader->model('user');
		$user->SelectUserFromSql($id);
		$this->smarty->assign('id',$id);
		if(isset($_POST['edit'])){
			$p1=sha1($_POST['password_1']);
			$p2=sha1($_POST['password_2']);
			if($p1==$p2){
				$user -> UpdatePasswordInSql($p1, $id);
				Routing::redirect('?q=user/show/'.$id);
			}
			else {
				$errors['passwordnotequal']='Nem egyezik!';
			}
		}
		$this->smarty->assign('id',$id);
		$this->smarty->assign('errors',$errors);
		$this->smarty->assign('user', $user);
		$this->smarty->assign('session_id',$_SESSION['user_id']);
		$this->smarty->assign('title', 'Kémia éve - Jelszó reset');
	}
	
	public function showall(){
		$user = $this->loader->model('user');
		$users = $user->SelectAllUserFromSql();
		$usercount = count($users);
		$this->smarty->assign('users', $users);
		$this->smarty->assign('usercount', $usercount);
		$this->smarty->assign('user_id',$_SESSION['user_id']);
		$this->smarty->assign('title', 'Kémia éve - Felhasználók');
	}
	
	public function manageusers(){
		$user = $this->loader->model('user');
		$users = $user->SelectAllUserFromSql();
		$usercount = count($users);
		if (!empty($_POST)){
			$temp=$_POST;
			//eredeti régi db
			for ($i=0 ; $i<$temp['oldcount'] ; $i++){
				$old[$i]['id']=$temp[$i.'-id'];
				$old[$i]['old_is_teacher']=$temp[$i.'_old-is_teacher'];
				if ($temp[$i.'-is_teacher'] == NULL)
					$old[$i]['is_teacher']='0';
				else $old[$i]['is_teacher']='1';
			}
			//változások a régihez képest
			for ($i=0 ; $i<$temp['oldcount'] ; $i++){
				if ($old[$i]['old_is_teacher']!=$old[$i]['is_teacher'])
				{	
					$dif[$old[$i]['id']]['id']=$old[$i]['id'];
					$dif[$old[$i]['id']]['is_teacher']=$old[$i]['is_teacher'];
				}
			}
			$user->UpdatePrivilegeInSql($dif);
			Routing::redirect('?q=user/manageusers');
		}
		$this->smarty->assign('users', $users);
		$this->smarty->assign('usercount', $usercount);
		$this->smarty->assign('session_is_teacher', $_SESSION['is_teacher']);
		$this->smarty->assign('title', 'Kémia éve - Felhasználók Jogai');
	}		
	
	public function edit($id){
		$user = $this->loader->model('user');
		$user->SelectUserFromSql($id);
		if (!empty($_POST)){
			$user->SetUsername($_POST['username']);
			$user->SetName($_POST['name']);
			$user->SetEmail($_POST['email']);
			$user->SetInfo($_POST['info']);
			$user->SetSchool($_POST['school']);
			$user->SetClassyear($_POST['classyear']);
			$user->SetBirthdate($_POST['birthdate']);
			$error=$user->UpdateUserInSql();
			if($error[0] == NULL ) Routing::redirect('?q=user/show/'.$id);
		}
		$this->smarty->assign('user', $user);
		$this->smarty->assign('id',$id);
		$this->smarty->assign('session_id',$_SESSION['user_id']);
		$this->smarty->assign('title', 'Kémia éve - Profil Módosítása');
		$this->smarty->assign('error',$error);
	}
	
	public function newuser(){
		$user = $this->loader->model('user');
		if (!empty($_POST)){
			$user->setId($_POST['id']);
			$user->setUsername($_POST['username']);
			$user->setName($_POST['name']);
			$user->setPassword($_POST['password']);
			$user->setEmail($_POST['email']);
			$user->SetSchool($_POST['school']);
			$user->SetClassyear($_POST['classyear']);
			$user->SetBirthdate($_POST['birthdate']);
			$errors=$user->InsertUserToSql();
			if((strlen($_POST['password'])) < 5 ){
				$errors['password'] = "A jelszónak legalább 5 karaktert kell tartalmaznia!";
			}
			if($errors[0] == '00000' ) {
				$user->CheckLogin();
				Routing::redirect('/');
			}
		}
		$this->smarty->assign('user', $user);
		$this->smarty->assign('title', 'Kémia éve - Új Felhasználó');
		$this->smarty->assign('errors', $errors);
	}
	
	public function login(){
		$user = $this->loader->model('user');
		if (!empty($_POST)){
			$user->setUsername($_POST['username']);
			$user->setPassword($_POST['password']);
			$error=$user->CheckLogin();
			if($error==NULL){
			Routing::redirect('/');
			}
			else{
				$loginerror=$error;
				//Routing::redirect('/');
			}
		}
		$this->smarty->assign('loginerror', $loginerror);
		$this->smarty->assign('session', $_SESSION['username']);
		$this->smarty->assign('user_id', $_SESSION['user_id']);
		$this->smarty->assign('title', 'Kémia éve - Bejelentkezés');
	}
	
	public function logout(){
		$user = $this->loader->model('user');
		$this->smarty->assign('title', 'Kémia éve - Kijelentkezés');
		$user->Logout();
		Routing::redirect('/');
	}
	
	public function score($id){
		$user = $this->loader->model('user');
		$uploadscore=$user->ScoreUploaded($id);
		$user = $user->Score($id);
		foreach($uploadscore as $us){
			$j=$us['tasksheet_id'];
			$score[$j][0] = $us['score'];
			$score[$j][1] = $us['name'];
			$score[$j][2] = $us['max'];
		}
		$answercount = count($user);
		for ($i = 0 ; $i < $answercount ; $i++)
		{
			$j=$user[$i]['id'];
			if (($user[$i]['is_correct'] == '1') && ($user[$i]['is_checkbox'] == '0'))
				$score[$j][0] = $score[$user[$i]['id']][0] + $user[$i]['score'];
			else if (($user[$i]['is_correct'] == '1') && ($user[$i]['is_checkbox'] == '1'))
				$score[$j][0] = $score[$user[$i]['id']][0] + $user[$i]['score']/$user[$i]['correct_checkbox'];
			$score[$j][1] = $user[$i]['name'];
			$score[$j][2] = $user[$i]['total_score'];
		}
		$this->smarty->assign('answers', $answercount);
		$this->smarty->assign('user', $user);
		$this->smarty->assign('scoretable', $score);
		$this->smarty->assign('title', 'Kémia éve - Pontok');
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);
	}
	
	public function scoreall(){
		$user = $this->loader->model('user');
		$user = $user->ScoreAll();
		$answercount = count($user);
		for ($i = 0 ; $i < $answercount ; $i++)
		{
			$j=$user[$i]['id'];
			if (($user[$i]['is_correct'] == '1') && ($user[$i]['is_checkbox'] == '0'))
				$score[$j][0] = $score[$j][0] + $user[$i]['score'];
			else if (($user[$i]['is_correct'] == '1') && ($user[$i]['is_checkbox'] == '1'))
				$score[$j][0] = $score[$j][0] + $user[$i]['score']/$user[$i]['correct_checkbox'];
			$score[$j][1] = $user[$i]['user'];
			$score[$j][2] = $user[$i]['id'];
			$score[$j][3] = $user[$i]['email'];
		}
		$usercount = count($score);
		$this->smarty->assign('users', $usercount);
		$this->smarty->assign('score',$score);
		$this->smarty->assign('title', 'Kémia éve - Versenyzők Pontjai');
		$this->smarty->assign('session_id',$_SESSION['user_id']);	
		$this->smarty->assign('session_is_teacher',$_SESSION['is_teacher']);		
	}
	
	public function delete($id){
		$user = $this->loader->model('user');
		///////
		///////
		///////			
		$this->smarty->assign('title', 'Kémia éve - Profil Törlése');	
	}
	
}
